package com.smalls.newvideotwo.mvp.model;

import android.os.Handler;

import com.smalls.newvideotwo.Constants;
import com.smalls.newvideotwo.NewDTVApp;
import com.smalls.newvideotwo.mvp.base.BaseBeanResult;
import com.smalls.newvideotwo.mvp.beanResult.GetChannelsResult;
import com.smalls.newvideotwo.mvp.http.Result;
import com.smalls.newvideotwo.mvp.model.modelInterface.IGetChannel;
import com.smalls.newvideotwo.util.LogUtil;
import com.smalls.newvideotwo.util.UIHelper;
import com.smalls.newvideotwo.utils.Constant;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by hs on 2018/3/20.
 */

public class GetChannelModel implements IGetChannel {
    private static String TAG = Constant.baddcoolio2tag? Constant.TAGPREFIXSTR + "GetMovieDetailModel":"GetMovieDetailModel";
    Handler handler = new Handler();

    @Override
    public void getChannel(final BaseBeanResult baseBeanResult,int s,int end) {
        LogUtil.d(this.TAG, "getChannel");
        Request request = null;
        try {
            request = new Request.Builder()
                    .addHeader("Content-Type", "application/json")
                    .addHeader("Cache-Control", "no-cache")
                    .addHeader("Authorization", "Bearer " + NewDTVApp.getApp().getToken())
                    .addHeader("Range",s+"-"+end)
                    .url(Constants.API_CHANNEL())
                    .get()
                    .build();
        }catch (java.lang.IllegalArgumentException e) {
            e.printStackTrace();
            baseBeanResult.onError(e);
            UIHelper.toastServerConnectErr();
            return;
        }

        new OkHttpClient.Builder()
                .addInterceptor(NewDTVApp.getApp().loggingInterceptor)
                .build()
                .newCall(request)
                .enqueue(new okhttp3.Callback() {
                    @Override
                    public void onFailure(Call call, final IOException e) {
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                baseBeanResult.onError(e);
                                UIHelper.toastServerConnectErr();
                            }
                        });

                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        final String resultStr = response.body().string();
                        handler.post(new Runnable() {
                            @Override
                            public void run() {

                                try {
                                    String addData = "{data:"+resultStr+"}";
                                    GetChannelsResult result = Result.getResult(addData, GetChannelsResult.class);
                                    if (result!=null) {
                                        baseBeanResult.onNext(result);
                                        return;
                                    }
                                    /// toastServerResponseErr(result.errorMessage);
                                } catch (Exception e) {
                                    e.printStackTrace();
                                    baseBeanResult.onError(e);
                                    UIHelper.toastServerResponseErr("something wrong with data.");
                                }
                            }
                        });

                    }
                });
    }
}
